package net.ewon.avatar.agent.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.commons.io.FileUtils;

import net.ewon.avatar.agent.AvatarPackageRepo;
import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.util.FilePathUtils;

public class RuntimeLinuxEnvironment extends RuntimeAbstractEnvironment {

	public RuntimeLinuxEnvironment(String root,
			AvatarPackageRepo avatarPackageRepo) {
		super(root, avatarPackageRepo);
	}

	protected void createLink(String envPath, AvatarPackage avatarPackage) throws IOException{
		Path source = Paths.get(normalize(avatarPackageRepo
				.getNormalizedBaseLocation()
				+ File.separator
				+ avatarPackage.getName()
				+ File.separator
				+ avatarPackage.getVersion()));
		
		 Path target = Paths.get(envPath);
		 LinkFiles lf = new LinkFiles(source, target);
		 Files.walkFileTree(source, lf);
	}

	protected void doShutdown(String envPath) throws IOException {
		String binDir = envPath + File.separator + "bin";
		runCommand(binDir, "./shutdown.sh");
	}

	protected void doStartup(String envPath) throws IOException {
		String binDir = envPath + File.separator + "bin";
		runCommand(binDir, "./startup.sh");
	}

	protected void doPurge(String envPath) throws IOException{
		FileUtils.forceDelete(new File(envPath));
	}

	@Override
	protected String normalize(String path) {
		return FilePathUtils.normalizeLinux(path);
	} 

}
